﻿<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
	<title xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory">Chapter 18. Inlined Classes</title>
	<link rel="stylesheet" href="css/espertech.css" type="text/css">
	<meta xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" name="generator" content="DocBook XSL-NS Stylesheets V1.74.0">
	<meta xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<link rel="home" href="index.html" title="Esper Reference">
	<link rel="up" href="index.html" title="Esper Reference">
	<link rel="prev" href="configuration.html" title="Chapter 17. Configuration">
	<link rel="next" href="script.html" title="Chapter 19. Script Support">
</head>

<body>
	<p xmlns:d="http://docbook.org/ns/docbook" id="title"><a href="./index.html" class="site_href"><strong>www.espertech.com</strong></a><a href="http://www.espertech.com/esper/esper-documentation/" class="doc_href"><strong>Documentation</strong></a></p>
	<ul xmlns:d="http://docbook.org/ns/docbook" class="docnav">
		<li class="previous"><a accesskey="p" href="configuration.html"><strong>Prev</strong></a></li>
		<li class="next"><a accesskey="n" href="script.html"><strong>Next</strong></a></li>
	</ul>
	<div class="chapter" lang="en-US">
		<div class="titlepage">
			<div>
				<div>
					<h2 class="title"><a id="inlinedclass"></a>Chapter 18. Inlined Classes</h2>
				</div>
			</div>
		</div>
		<div class="toc">
			<dl>
				<dt><span class="sect1"><a href="inlinedclass.html#inlinedclass-overview">18.1. Overview</a></span></dt>
				<dt><span class="sect1"><a href="inlinedclass.html#inlinedclass-local">18.2. Declaring a Local Class</a></span></dt>
				<dt><span class="sect1"><a href="inlinedclass.html#inlinedclass-create">18.3. Declaring a Global Class</a></span></dt>
				<dt><span class="sect1"><a href="inlinedclass.html#inlinedclass-performance">18.4. Performance Notes</a></span></dt>
				<dt><span class="sect1"><a href="inlinedclass.html#inlinedclass-limitations">18.5. Known Limitations</a></span></dt>
			</dl>
		</div>
		<div class="sect1" lang="en-US">
			<div class="titlepage">
				<div>
					<div>
						<h2 class="title"><a id="inlinedclass-overview"></a>18.1. Overview</h2>
					</div>
				</div>
			</div><a id="d0e49809" class="indexterm"></a>
			<p>
				Your application can add Java class code (or C# class code for NEsper) directly into the EPL. The EPL compiler compiles the classes your application provides and makes the classes available for use
				within EPL. Classes can be local i.e. visible to the current module only, or global i.e. visible to other modules according to EPL-object visibility rules.
				The typical use of classes is to perform imperative programming or provide utility, integration or extension functionality.
			</p>
			<p>
				For declaring classes that are visible across multiple statements i.e. globally visible classes please consult <a class="xref" href="inlinedclass.html#inlinedclass-create" title="18.3. Declaring a Global Class">Section 18.3, “Declaring a Global Class”</a>
				that explains the <code class="literal">create inlined_class</code> clause. Inlined-classes are EPL-objects following visibility rules according to <a class="xref" href="apicompiler.html#apicompiler-concepts-eplobjects" title="15.2.2. EPL-objects">Section 15.2.2, “EPL-objects”</a>.
			</p>
			<p>
				Inlined classes can provide single-row functions as described in <a class="xref" href="extension.html#custom-singlerow-inlined" title="22.2.1. Using an Inlined Class to Provide a Single-Row Function">Section 22.2.1, “Using an Inlined Class to Provide a Single-Row Function”</a>.
			</p>
			<p>
				Inlined classes can provide aggregation single-functions as described in <a class="xref" href="extension.html#custom-aggregation-inlined" title="22.5.1.4. Using an Inlined Class to Provide an Aggregation Single-Function">Section 22.5.1.4, “Using an Inlined Class to Provide an Aggregation Single-Function”</a>.
			</p>
			<p>
				Inlined classes can provide aggregation multi-functions as described in <a class="xref" href="extension.html#custom-aggregation-multi-inlined" title="22.5.2.8. Using an Inlined Class to Provide an Aggregation Multi-Function">Section 22.5.2.8, “Using an Inlined Class to Provide an Aggregation Multi-Function”</a>.
			</p>
			<p>
				Inlined classes can serve as a variable type. The <code class="literal">create variable</code> clause allows the class name of an inlined class as a variable type.
			</p>
		</div>
		<div class="sect1" lang="en-US">
			<div class="titlepage">
				<div>
					<div>
						<h2 class="title"><a id="inlinedclass-local"></a>18.2. Declaring a Local Class</h2>
					</div>
				</div>
			</div>
			<p>
				A local inlined class is a class that is only visible to the same EPL statement that it is part of. The class is not visible to other EPL statements in the same module and is not visible to other modules.
			</p>
			<p>
				The syntax for inlined classes is:
			</p>
			<pre class="synopsis">inlined_class <code class="literal">"""</code> <span class="emphasis"><em>class-text</em></span> <code class="literal">"""</code></pre>
			<p>
				Use the <code class="literal">inlined_class</code> keyword to declare a class. Place the <span class="emphasis"><em>class-text</em></span> class code within three double-quotes (<code class="literal">"""</code>).
			</p>
			<p>
				The <span class="emphasis"><em>class-text</em></span> class code is a Java class, i.e. <code class="literal">public class</code> <span class="emphasis"><em>class-name</em></span><code class="literal">{...}</code> or <code class="literal">public enum</code> <span class="emphasis"><em>class-name</em></span><code class="literal">{...}</code>.
			</p>
			<p>
				The class text may specify a package name and imports. The class text is subject to limitations as listed below.
			</p>
			<p>
				The next example shows a statement that calls a static method of an inlined-class by name <code class="literal">MyUtility</code> which computes the Fibonacci total for a given number:
			</p>
			<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">inlined_class """
  public class MyUtility {
    public static double fib(int n) {
      if (n &lt;= 1)
        return n;
      return fib(n-1) + fib(n-2);
    }
  }
"""
select MyUtility.fib(intValue) from SomeEvent</pre>
		</div>
		<div class="sect1" lang="en-US">
			<div class="titlepage">
				<div>
					<div>
						<h2 class="title"><a id="inlinedclass-create"></a>18.3. Declaring a Global Class</h2>
					</div>
				</div>
			</div>
			<p>
				Use the <code class="literal">create inlined_class</code> syntax to declare an inlined class. The compiler remembers the class
				and allows the class to be referenced by other statements of the same module and by other modules.
			</p>
			<p>
				The below EPL declares a globally visible class by name <code class="literal">MyUtility </code> that provides a method <code class="literal">midPrice</code> that computes a mid-price and that a buy and sell parameter:
			</p>
			<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">create inlined_class """ 
  public class MyUtility {
    public static double midPrice(double buy, double sell) {
      return (buy + sell) / 2;
    }
  }
"""</pre>
			<p>
				The next EPL returns mid-price for each event:
			</p>
			<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select MyUtility.midPrice(buy, sell) from MarketDataEvent</pre>
			<p>
				The class name must be unique for global inlined classes. It is not possible to declare the same global inlined-class twice with the same class name and the same module name.
			</p>
			<p>
				The runtime, at the time your application deploys a module that references global inlined classes, provides a classloader that adds the imported inlined classes that are provided by the deployment that contains the inlined classes. Thus the compiler and runtime never copy or otherwise duplicate the inlined class or class byte code.
			</p>
		</div>
		<div class="sect1" lang="en-US">
			<div class="titlepage">
				<div>
					<div>
						<h2 class="title"><a id="inlinedclass-performance"></a>18.4. Performance Notes</h2>
					</div>
				</div>
			</div>
			<p>
				The EPL compiler compiles the inlined classes to byte code and the runtime loads the byte code using a classloader that is aware of the inlined classes.
			</p>
			<p>
				It is recommended that inlined class code executes non-blocking.
			</p>
			<p>
				It is recommended that inlined class code is stateless. Please use plug-in aggregation functions or variables instead.
			</p>
			<p>
				It is recommended that inlined class code is thread safe. EPL execution generally allows certain EPL statements and partitions to execute lock-free.
			</p>
		</div>
		<div class="sect1" lang="en-US">
			<div class="titlepage">
				<div>
					<div>
						<h2 class="title"><a id="inlinedclass-limitations"></a>18.5. Known Limitations</h2>
					</div>
				</div>
			</div>
			<p>
				The compiler for inlined classes is Janino. Janino does not support all of Java 17 and higher. Please see the Janino documentation for more information.
			</p>
			<p>
				The following limitations apply:
			</p>
			<div class="itemizedlist">
				<ul>
					<li>
						<p>Annotations; an inlined class cannot be an annotation</p>
					</li>
					<li>
						<p>Listeners or subscribers; an inlined class cannot be a listener or subscriber</p>
					</li>
					<li>
						<p>Insert-into; for <code class="literal">insert into</code>, your application cannot populate an instance of an inlined class</p>
					</li>
					<li>
						<p>Event type; for <code class="literal">create schema</code>, an inlined class cannot be a property type and cannot be an event type</p>
					</li>
					<li>
						<p>Named windows and tables; for <code class="literal">create window</code> and for <code class="literal">create table</code>, an inlined class cannot be a column type</p>
					</li>
					<li>
						<p>The <code class="literal">@type</code> annotation cannot be used with inlined classes</p>
					</li>
					<li>
						<p>Inlined classes cannot be view, enumeration method, virtual data window or pattern extension classes</p>
					</li>
					<li>
						<p>Script may not use inlined classes</p>
					</li>
				</ul>
			</div>
		</div>
	</div>
	<ul xmlns:d="http://docbook.org/ns/docbook" class="docnav">
		<li class="previous"><a accesskey="p" href="configuration.html"><strong>Prev</strong>Chapter 17. Configuration</a></li>
		<li class="up"><a accesskey="u" href="#"><strong>Top of page</strong></a></li>
		<li class="home"><a accesskey="h" href="index.html"><strong>Front page</strong></a></li>
		<li class="next"><a accesskey="n" href="script.html"><strong>Next</strong>Chapter 19. Script Support</a></li>
	</ul>
</body>

</html>